package com.luyue.zagj.activity.home;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.jude.easyrecyclerview.decoration.SpaceDecoration;
import com.luyue.zagj.R;
import com.luyue.zagj.BaseActivity;
import com.luyue.zagj.adapter.NewsAdapter;
import com.luyue.zagj.domin.News;
import com.luyue.zagj.domin.NewsGson;
import com.luyue.zagj.httpData.DemoApi;
import com.luyue.zagj.utils.PixUtil;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

public class TravelRecordActivity extends BaseActivity {


    @Bind(R.id.travel_record_cancel_btn)
    Button travelRecordCancelBtn;
    @Bind(R.id.travel_record_top_rl)
    RelativeLayout travelRecordTopRl;
    @Bind(R.id.travel_record_days_all_tv)
    TextView travelRecordDaysAllTv;
    @Bind(R.id.travel_record_days_err_tv)
    TextView travelRecordDaysErrTv;
    @Bind(R.id.travel_record_today_all_tv)
    TextView travelRecordTodayAllTv;
    @Bind(R.id.travel_record_today_err_tv)
    TextView travelRecordTodayErrTv;
    @Bind(R.id.travel_record_lv)
    EasyRecyclerView travelRecordLv;

    private int page = 0;
    //真数据时候换掉
    private NewsAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_travel_record);
        ButterKnife.bind(this);


        setLv();
        initData();

    }


    private void setLv() {
        travelRecordLv.setAdapter(adapter = new NewsAdapter(this));
        travelRecordLv.setLayoutManager(new LinearLayoutManager(this));
        //添加边框
        SpaceDecoration itemDecoration = new SpaceDecoration((int) PixUtil.convertDpToPixel(0, this));
        itemDecoration.setPaddingEdgeSide(true);
        itemDecoration.setPaddingStart(true);
        itemDecoration.setPaddingHeaderFooter(false);
        travelRecordLv.addItemDecoration(itemDecoration);

        //更多加载
        adapter.setMore(R.layout.view_more, new RecyclerArrayAdapter.OnMoreListener() {
            @Override
            public void onMoreShow() {
                initData();
            }

            @Override
            public void onMoreClick() {

            }
        });
        //写刷新事件
        travelRecordLv.setRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                travelRecordLv.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        adapter.clear();
                        page = 0;
                        initData();
                    }
                }, 1000);
            }
        });

        //点击事件
        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
//                ArrayList<String> data = new ArrayList<String>();
//                data.add(adapter.getAllData().get(position).getPicUrl());
//                data.add(adapter.getAllData().get(position).getUrl());
//                Intent intent = new Intent(getActivity(), NewsDetailsActivity.class);
//                //用Bundle携带数据
//                Bundle bundle = new Bundle();
//                bundle.putStringArrayList("data", data);
//                intent.putExtras(bundle);
//                startActivity(intent);
            }
        });
    }

    private void initData() {
        Log.d("page", page + "");
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://api.tianapi.com/")
                //String
                .addConverterFactory(ScalarsConverterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())//添加 json 转换器
                //    compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())//添加 RxJava 适配器
                .build();

        DemoApi apiManager = retrofit.create(DemoApi.class);//这里采用的是Java的动态代理模式

        apiManager.getNewsData("528f49d2debfd453585f9da6a679c0a1", "10", page)
                .subscribeOn(Schedulers.io())
                .map(new Func1<NewsGson, List<News>>() {
                    @Override
                    public List<News> call(NewsGson newsgson) { //
                        List<News> newsList = new ArrayList<News>();
                        for (NewsGson.NewslistBean newslistBean : newsgson.getNewslist()) {
                            News new1 = new News();
                            new1.setTitle(newslistBean.getTitle());
                            new1.setCtime(newslistBean.getCtime());
                            new1.setDescription(newslistBean.getDescription());
                            new1.setPicUrl(newslistBean.getPicUrl());
                            new1.setUrl(newslistBean.getUrl());
                            newsList.add(new1);
                        }
                        return newsList; // 返回类型
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<List<News>>() {
                    @Override
                    public void onNext(List<News> newsList) {
                        adapter.addAll(newsList);
                    }

                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        Toast.makeText(TravelRecordActivity.this,
                                "网络连接失败", Toast.LENGTH_LONG).show();
                    }
                });
        page = page + 1;
    }


    @OnClick(R.id.travel_record_cancel_btn)
    public void onClick() {
        onBackPressed();
    }
}
